#include <gtk/gtk.h>
#include "portugues.h"
#include "cliente.h"


GtkBuilder *builder;
GtkWidget *main_window;
GtkWidget *statusbar;
GtkWidget *buttons[3]; //Enviar, Limpar e Anunciar
GtkWidget *textviews[2];
GtkWidget *entry;

void on_button1_clicked(GtkButton *button, GtkWidget *textview) {
	GtkTextBuffer *buffer1, *buffer2;
	GtkTextIter start, end;
	int a;

	//Pega o que foi digitado
	buffer1 =  gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
	buffer2 =  gtk_text_view_get_buffer(GTK_TEXT_VIEW(textviews[0]));	
	//As linhas abaixo servem para capturar o texto digitado e iniciar o cliente.
	gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER (buffer1), &start);
	gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER (buffer1), &end);
	gchar *ttt = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer1), &start, &end, TRUE);
	a = strlen(ttt);
	//gtk_text_buffer_set_text(buffer2, ttt, a);
	gtk_text_buffer_insert_at_cursor(buffer2, ttt, a);
	gtk_text_buffer_insert_at_cursor(buffer2, "\n", 1);
	gtk_text_buffer_set_text(buffer1, "", 0);
	g_print("Texto: %s \n", ttt);
	inicio(ttt);
	
		
}

void on_button2_clicked(GtkButton *button, GtkWidget *textview) {
	//gtk_text_buffer_set_text((gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview))), "", -1);
	gtk_statusbar_push(GTK_STATUSBAR(statusbar), gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), ""), SEARCHING_HOSTS);
}

void initWidgets( ) {
	main_window = GTK_WIDGET(gtk_builder_get_object(builder, "main_window"));
	statusbar = GTK_WIDGET(gtk_builder_get_object(builder, "statusbar1"));
	textviews[0] = GTK_WIDGET(gtk_builder_get_object(builder, "textview1")); //caixa de cima
	textviews[1] = GTK_WIDGET(gtk_builder_get_object(builder, "textview2")); //caixa de baixo
	buttons[0] = GTK_WIDGET(gtk_builder_get_object(builder, "button1"));
	buttons[1] = GTK_WIDGET(gtk_builder_get_object(builder, "button2"));
	buttons[2] = GTK_WIDGET(gtk_builder_get_object(builder, "button3"));
	entry = GTK_WIDGET(gtk_builder_get_object(builder, "entry1"));
	
	gtk_button_set_label(GTK_BUTTON(buttons[0]), BUTTON_SEND);
	gtk_button_set_label(GTK_BUTTON(buttons[1]), BUTTON_CLEAR);
	gtk_button_set_label(GTK_BUTTON(buttons[2]), BUTTON_ANNOUNCE);
	
	gtk_statusbar_push(
		GTK_STATUSBAR(statusbar),
		gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), ""),
		TYPE_NAME
	);
}

void connectSignals( ) {
	g_signal_connect_swapped(G_OBJECT(main_window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(main_window));
	g_signal_connect(GTK_BUTTON(buttons[0]), "clicked", G_CALLBACK(on_button1_clicked), GTK_TEXT_VIEW(textviews[1]));
	g_signal_connect(GTK_BUTTON(buttons[1]), "clicked", G_CALLBACK(on_button2_clicked), GTK_ENTRY(entry));
}

void createGUI(int *argc, char ***argv) {
	gtk_init(argc, argv);
	builder = gtk_builder_new( );
	gtk_builder_add_from_file (builder, "if-chat.xml", NULL);
	gtk_builder_connect_signals(builder, NULL);
	initWidgets( );
	connectSignals( );
	g_object_unref(G_OBJECT(builder));
	gtk_widget_show(main_window);                
	gtk_main( );
}
